#!/bin/bash
# - rootfs
# - benchmark

## makepkg is a script that automates the building of packages; it can download and validate source files,
## check dependencies, configure build-time settings, compile the sources, install into a temporary root,
## make customizations, generate meta-info, and package everything together.
## See: https://wiki.archlinux.org/index.php/Makepkg

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/misc-base.sh
. $LKP_SRC/lib/env.sh
. $LKP_SRC/lib/reproduce-log.sh
. $LKP_SRC/lib/lkp_path.sh

[ -n "$rootfs" ] || die "rootfs is empty"
[ -n "$benchmark" ] || die "benchmark is empty"

export DISTRO=${rootfs%%-*}
export GIT_SSL_NO_VERIFY=true

. $LKP_SRC/distro/${DISTRO}
. $LKP_SRC/lib/install.sh

cd $LKP_SRC/pkg/$benchmark || die "pkg is empty"

pack_to=$(basename $rootfs)
PKG_MNT=/osimage/pkg
mkdir -p "$PKG_MNT"

[ -n "$LKP_SERVER" ] && {
	mount $LKP_SERVER:$PKG_MNT $PKG_MNT || die "Failed to run mount"
}

get_pkg_info()
{
	var=$1
	pkg_dir=${2:-.}
	(
		. $pkg_dir/PKGBUILD
		eval echo '$'$var
	)
}

# for packages with same dependencies, force the update instead of generating link
# due to schedule stage will auto detect symbolic link, so no need generate new link
check_shared_pkg()
{
	if [ -L $LKP_SRC/pkg/$benchmark ]; then
		basename $(readlink $LKP_SRC/pkg/$benchmark)
	else
		echo $benchmark
	fi
}

update_shared_pkg()
{
	local bm_link=$1

	[ "$bm_name" = "$benchmark" ] && return

	# benchmark is a symlink
	ln -sf "$bm_link" "$sync_dest/${benchmark}-${arch}.cgz" || return
	echo "update shared pkg link ${benchmark}-${arch}.cgz -> $bm_link"
}

distro_install_depends lkp-dev
distro_install_depends ${benchmark}-dev

[[ $arch ]] || arch=$(get_system_arch)

build_depends_pkg -i $benchmark $TMP

echo $LKP_SRC/sbin/makepkg --config $(lkp_src)/etc/makepkg.conf
sync_dest="$PKG_MNT/$pack_to"
date=$(date +"%Y%m%d")
pkgver=$(get_pkg_info pkgver)
pkgrel=$(get_pkg_info pkgrel)
bm_name=$(check_shared_pkg)
cgz_name="${bm_name}-${arch}-${pkgver:-0}-${pkgrel:-0}_${date}.cgz"

setup_proxy

PACMAN=true BUILDDIR=$TMP CARCH=$arch PKGEXT=.cgz CGZDEST="$sync_dest/$cgz_name" \
	$LKP_SRC/sbin/makepkg --config $(lkp_src)/etc/makepkg.conf

update_softlink()
{
	[ -e "$sync_dest/$cgz_name" ] || return

	ln -sf "$(basename $(realpath $sync_dest/$cgz_name))" "$sync_dest/${bm_name}-${arch}.cgz" || return
	echo "create package: $sync_dest/${bm_name}-${arch}.cgz -> $(realpath $sync_dest/$cgz_name)"

	update_shared_pkg "${bm_name}-${arch}.cgz"

	[ -L $sync_dest/$cgz_name ] || return 0
	rm $sync_dest/$cgz_name
}

update_softlink && echo "Makepkg finished successfully"
